#include "types.h"
#include "riscv.h"
#include "defs.h"

volatile static int started = 0;

// start() jumps here in supervisor mode on all CPUs.
void
main()
{
    if(cpuid() == 0){
        uartinit();
        printfinit();
        printf("\n");
        printf("xv6 kernel is booting\n");
        printf("\n");
        kinit();         // physical page allocator
        kvminit();       // create kernel page table
        kvminithart();   // turn on paging
        procinit();
        trapinithart();  // install kernel trap vector
        plicinit();      // set up interrupt controller
        plicinithart();  // ask PLIC for device interrupts
        //virtio_disk_init(); // emulated hard disk
        kproinit1();
        kproinit2();
        __sync_synchronize(); // __sync_synchronize() 是一个 GCC 内建函数，它扮演着内存屏障（memory barrier）的角色。它的作用是确保代码中这条指令前后的内存操作不会被编译器或处理器重排
        started = 1;
    }else{
        while(started == 0)  //等待CPU0初始化完成
              ;
        __sync_synchronize();
        printf("hart %d starting\n", cpuid());
        kvminithart();
        trapinithart();   // install kernel trap vector
        plicinithart();
    }

    scheduler();
}
